<?php

namespace App\Http\Controllers\Home;

use App\Model\Lesson;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LessonController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $lessons=Lesson::paginate(10);
        return view('home.lesson.index',compact('lessons'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $field=[
            'lesson'=>[
                'title'=>'','description'=>'','thumb'=>asset('org/hdjs/image/nopic.jpg')
            ],
            'video'=>[]
        ];
        return view('home.lesson.create',compact('field'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,Lesson $lesson)
    {
        $data=json_decode($request->field,true);
//        dd($data);
        $this->validateData($data['lesson']);
        $lesson=auth()->user()->lesson()->create($data['lesson']);
        $lesson->video()->createMany($data['video']);
        return redirect()->route('lesson.lists')->with('success','添加成功');
    }




    public function show(Lesson $lesson)
    {
        return view('home.lesson.show',compact('lesson'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Model\Lesson  $lesson
     * @return \Illuminate\Http\Response
     */
    public function edit(Lesson $lesson)
    {
        $field=[
            'lesson'=>$lesson->toArray(),
            'video'=>$lesson->video->toArray()
        ];
        return view('home.lesson.edit',compact('field'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Model\Lesson  $lesson
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Lesson $lesson)
    {
        $data=json_decode($request->field,true);
//        dd($data);
        $this->validateData($data['lesson']);
//        dd($lesson->toArray());
        $lesson->update($data['lesson']);
//        dd($lesson->toArray());
        $lesson->video()->delete();
//        dd(1);
        foreach ($data['video'] as $video){
//            dd($lesson->video()->withTrashed()->get());
            $lesson->video()->withTrashed()->updateOrCreate ( [ 'id' => $video['id']??0],$video )->restore();
        }
        return redirect()->route('lesson.lists')->with('success','更新成功');
    }


    public function destroy(Lesson $lesson)
    {
        $lesson->delete();
        $lesson->video()->forceDelete();
        return back()->with('success','删除成功');
    }

    public function lists(){
//        dd(111);
        $lessons=Lesson::get();
        return view('home.lesson.lists',compact('lessons'));
    }

    public function validateData($data){
        \Validator::make($data,[
            'title'=>'required',
            'description'=>'required',
            'thumb'=>'required'
        ],[
            'title.required'=>'请输入标题',
            'description.required'=>'请输入课程描述',
            'thumb.required'=>'请上传缩略图'
        ])-> validate ();
    }
}
